﻿using System;
using System.Linq;
using System.Linq.Expressions;
using Dgg.Anug.Cqrs.Core.Domain.DefectHandling;
using Dgg.Anug.Cqrs.Core.Domain.DefectHandling.Events;
using Dgg.Anug.Cqrs.Core.Infrastructure.Data;
using Dgg.Anug.Cqrs.Core.Infrastructure.Eventing;

namespace Dgg.Anug.Cqrs.Core.Application.Admin.Services.Handlers
{
	public abstract class AdminEntityDeletedIssueSnapshotUpdater
	{
		private readonly ISnapshotSession _snapshotSession;
		private readonly EventBus<IssueDeleted> _bus;

		protected AdminEntityDeletedIssueSnapshotUpdater(ISnapshotSession snapshotSession, EventBus<IssueDeleted> bus)
		{
			_snapshotSession = snapshotSession;
			_bus = bus;
		}

		protected void deleteRelatedIssues(Expression<Func<Issue, bool>> relatedToEntity)
		{
			_snapshotSession.All<Issue>()
				.Where(relatedToEntity)
				.ToList()
				.ForEach(i => _bus.Publish(new IssueDeleted(i.Id)));
		}
	}
}
